package com.songqintuan.mall.customer;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ValueChangeEvent;

import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.City;
import com.songqintuan.model.po.County;
import com.songqintuan.model.po.Province;
import com.songqintuan.persist.service.hibernate.ICityService;
import com.songqintuan.persist.service.hibernate.IProvinceService;

public class ProvinceCitySelectBean implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */

	protected List<Province> provinces = new ArrayList<Province>();
	protected List<City> cities = new ArrayList<City>();
	protected List<County> counties = new ArrayList<County>();



	public ProvinceCitySelectBean(){
		
	}
	
	public void InitBean(Province pro) {
		Long proId = (null!= pro)?pro.getId():null;
		IProvinceService<Province,Long> proService = (IProvinceService<Province,Long>)SpringFacesUtil.getSpringBean(com.songqintuan.persist.constant.PersistConstants.PROVINCE_SERVICE_BEAN_NAME);
		proService.setEntityClass(Province.class);
		proService.setPKClass(Long.class);
		List<Province> allPro = proService.getAll();
		this.setProvinces(allPro);
		if (null != proId && (0 != (proId))) {
			ICityService<City,Long> cityService = (ICityService<City,Long>)SpringFacesUtil.getSpringBean(com.songqintuan.persist.constant.PersistConstants.CITY_SERVICE_BEAN_NAME);
			cityService.setEntityClass(City.class);
			cityService.setPKClass(Long.class);
			List<City> thisCity = cityService.findBy("provinceId",proId);
			this.setCities(thisCity);
		}
	}

	public List<Province> getProvinces() {
		return provinces;
	}

	public void setProvinces(List<Province> provinces) {
		this.provinces = provinces;
	}

	public List<City> getCities() {
		return cities;
	}

	public void setCities(List<City> cities) {
		this.cities = cities;
	}


	public String initCity(Province pro) {
		Long proId = (null!= pro)?pro.getId():null;
		if (null != proId && !("".equals(proId))) {
			ICityService<City,Long> cityService = (ICityService<City,Long>)SpringFacesUtil.getSpringBean(com.songqintuan.persist.constant.PersistConstants.CITY_SERVICE_BEAN_NAME);
			cityService.setEntityClass(City.class);
			cityService.setPKClass(Long.class);
			List<City> thisCity = cityService.findBy("provinceId", new Long(proId));
			this.setCities(thisCity);
		}
		return null;
	}
	public void provinceValueChange(ValueChangeEvent event){
		Long newValue =(Long) event.getNewValue();
		Long oldValue =(Long) event.getOldValue();
		if (!(newValue == oldValue)) {//���»�ȡcity
			ICityService<City,Long> cityService = (ICityService<City,Long>)SpringFacesUtil.getSpringBean(com.songqintuan.persist.constant.PersistConstants.CITY_SERVICE_BEAN_NAME);
			cityService.setEntityClass(City.class);
			cityService.setPKClass(Long.class);
			List<City> thisCity = cityService.findBy("provinceId", newValue);
			this.setCities(thisCity);
		}
		
	}
}
